﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrienDev.Data;
using FrienDev.BusinessRules;

public partial class Home_Registration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BintEvent();
        if (!IsPostBack)
        {
            InitDDLYear();
            InitDDLMonth();
            InitDDLDay(31);
        }

        User user = UserRules.Instance.IsUserRegisted();
        if (user != null)
        {
            Response.Redirect(ApplicationSettings.HOMEPAGEFILENAME);
        }
    }

    private void BintEvent()
    {
        ddl_month.SelectedIndexChanged += new EventHandler(ddl_month_SelectedIndexChanged);
        bt_Save.Click += new EventHandler(bt_Save_Click);
        txt_Email.Attributes.Add("onchange", "javascript:display();");
    }

    void bt_Save_Click(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(1000);
        lb_EmailExitingTip.Visible = false;
        DateTime birthday = Convert.ToDateTime(ddl_year.SelectedValue + "-" + ddl_month.SelectedValue + "-" + ddl_day.SelectedValue);
        if (UserRules.Instance.EmailUnique(txt_Email.Text))
        {
            try
            {
                UserRules.Instance.InsertUserBasicInfo(txt_name.Text, txt_Email.Text, Convert.ToBoolean(Convert.ToInt32(ddl_Gender.SelectedValue)), birthday, "", "");
                if (HttpContext.Current.Session["ActivityID"] != null)
                {
                    int activityId = Convert.ToInt32(HttpContext.Current.Session["ActivityID"]);
                    HttpContext.Current.Session.Remove("ActivityID");
                    Response.Redirect("~/Activity/detail.aspx?activityid=" + activityId);
                }
                if (HttpContext.Current.Session["InviteUserID"] != null)
                {
                    FriendRules.Instance.AcceptFriend(HttpContext.Current.Session["InviteUserID"].ToString(), UserRules.Instance.GetCurrentUserId());
                    HttpContext.Current.Session.Remove("InviteUserID");
                }
                Response.Redirect(ApplicationSettings.HOMEPAGEFILENAME);
            }
            catch (Exception)
            {
            }
        }
        else
        {
            lb_EmailExitingTip.Visible = true;
        }
        this.up_UserBasicInfoForm.Update();
    }

    void ddl_month_SelectedIndexChanged(object sender, EventArgs e)
    {
        int currentmonth = Convert.ToInt32(ddl_month.SelectedValue.ToString());
        if (currentmonth == 1 || currentmonth == 3 || currentmonth == 5 || currentmonth == 7 || currentmonth == 8 || currentmonth == 10 || currentmonth == 12)
        {
            InitDDLDay(31);
        }
        else
        {
            InitDDLDay(30);
        }
    }

    private void InitDDLYear()
    {
        ddl_year.Items.Clear();
        int i = 1900;
        while (i <= Convert.ToInt32(DateTime.Now.Year.ToString()))
        {
            ListItem yearitem = new ListItem();
            yearitem.Text = i.ToString();
            yearitem.Value = i.ToString();
            ddl_year.Items.Add(yearitem);
            i++;
        }
    }

    private void InitDDLMonth()
    {
        ddl_month.Items.Clear();
        int i;
        for (i = 1; i <= 12; i++)
        {
            ListItem monthitem = new ListItem();
            monthitem.Text = i.ToString();
            monthitem.Value = i.ToString();
            ddl_month.Items.Add(monthitem);
        }
    }

    private void InitDDLDay(int count)
    {
        ddl_day.Items.Clear();
        int i;
        for (i = 1; i <= count; i++)
        {
            ListItem dayitem = new ListItem();
            dayitem.Text = i.ToString();
            dayitem.Value = i.ToString();
            ddl_day.Items.Add(dayitem);
        }
    }
}
